﻿using Common.Log;
using DryWCS.App.Common;
using DryWCS.Model.Stacker;
using DryWCS.Model.WMS;
using DryWCS.Service.Conveyor;
using DryWCS.Service.Stacker;
using DryWCS.Service.WMS;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.App.ViewModel.Tasks
{
    public class AddStackerTaskViewModel : ViewModelBase
    {
        string msg = null;
        StackerTaskService stackerTaskService = new StackerTaskService();
        WareHouseCellInfoService wareHouseCellInfoService = new WareHouseCellInfoService();
        public StackerTaskEntity model;

        public StackerTaskEntity Model
        {
            get { return model; }
            set
            {
                model = value;
                this.RaisePropertyChanged("Model");
            }
        }

        private RelayCommand addTaskCommand;
        public RelayCommand AddTaskCommand
        {
            get
            {
                if (addTaskCommand == null)
                {
                    addTaskCommand = new RelayCommand(AddTask);
                }
                return addTaskCommand; ;
            }
            set { addTaskCommand = value; }
        }

        private void AddTask()
        {
            msg = Check();
            if (msg != null)
            {
                Msg.Info(msg);
                return;
            }
            StackerTaskEntity stackerTask = stackerTaskService.GetFirst();
            Model.CreateTime = DateTime.Now;
            Model.HasNextTask = false;
            Model.WMSOrderId = stackerTask.Id+1;
            Model.Status = 0;
            Model.HasNextTask = false;
            Model.NextTaskId = 0;
            
           
            if (stackerTaskService.Insert(Model))
            {
                msg = $"任务类型{Model.TaskType},起始设备{Model.StartMachineID},起始排列层{Model.StartLineNo}-{Model.StartColNo}-{Model.StartLayerNo},终点设备{Model.EndMachineID},终点排列层{Model.EndLineNo}-{Model.EndColNo}-{Model.EndLayerNo} 添加成功";
            }
            else
                msg = $"任务类型{Model.TaskType},起始设备{Model.StartMachineID},起始排列层{Model.StartLineNo}-{Model.StartColNo}-{Model.StartLayerNo},终点设备{Model.EndMachineID},终点排列层{Model.EndLineNo}-{Model.EndColNo}-{Model.EndLayerNo} 添加失败";
            Msg.Info(msg);
            LogHelper.WriteLog(msg);
        }

        #region 验证
        private string Check()
        {
            string ret = null;
            //if (!(Model.StartMachineID == 0 || Model.StartMachineID == 1001))
            //    return ret = $"起始设备{Model.StartMachineID}不存在";
            //if (!(Model.EndMachineID == 0 || Model.EndMachineID == 1002 || Model.EndMachineID == 1003))
            //    return ret = $"终点设备{Model.StartMachineID}不存在";
            if (!(Model.EndLineNo == 1 || Model.EndLineNo == 2 ||Model.EndLineNo==0))
                return ret = $"终点排{Model.EndLineNo}不存在";
            if (!(Model.StartLineNo == 1 || Model.StartLineNo == 2 || Model.StartLineNo == 0))
                return ret = $"起始排{Model.EndLineNo}不存在";
            if (!(Model.StartLayerNo > -1 && Model.StartLayerNo < 8))
                return ret = $"起始层{Model.StartLayerNo}不存在";
            if (!(Model.EndLayerNo > -1 && Model.EndLayerNo < 8))
                return ret = $"终点层{Model.EndLayerNo}不存在";
            if (!(Model.StartColNo > -1 && Model.StartColNo < 39))
                return ret = $"起始列{Model.StartColNo}不存在";
            if (!(Model.EndColNo > -1 && Model.EndColNo < 39))
                return ret = $"终点列{Model.EndColNo}不存在";
            if (!(Model.TaskType > 1 && Model.TaskType < 7))
                return ret = $"任务类型{Model.TaskType}不存在";
            if (Model.EndColNo != 0 && Model.EndLineNo != 0 && Model.EndLayerNo != 0)
            {
                WareHouseCellInfoModel cellInfoModel = wareHouseCellInfoService.GetBy(1,Model.EndLineNo,Model.EndColNo,Model.EndLayerNo);
                if (cellInfoModel != null)
                {
                    if (cellInfoModel.POS_STATUS != 0)
                    {
                        return ret = $"终点货位不是空！";
                    }
                }
            }
            if (Model.StartColNo != 0 && Model.StartLineNo != 0 && Model.StartLayerNo != 0)
            {
                WareHouseCellInfoModel cellInfoModel = wareHouseCellInfoService.GetBy(1, Model.StartLineNo, Model.StartColNo, Model.StartLayerNo);
                if (cellInfoModel != null)
                {
                    if (cellInfoModel.POS_STATUS == 0)
                    {
                        return ret = $"取货货位没有货物！";
                    }
                }
            }
            return ret;
        }
        #endregion
        public AddStackerTaskViewModel()
        {
            Model = new StackerTaskEntity();
        }
    }
}

